﻿using System;
using System.Collections.Generic;
using System.IO.IsolatedStorage;

namespace OverlakeApp
{
#if PC
    public static class Settings
    {
        private static readonly Dictionary<string, object> Dict = new Dictionary<string, object>();

        public static bool AddOrUpdateValue(string key, Object value)
        {
            bool valueChanged = false;

            // If the key exists
            if (Dict.ContainsKey(key))
            {
                // If the value has changed
                if (Dict[key] != value)
                {
                    // Store the new value
                    Dict[key] = value;
                    valueChanged = true;
                }
            }
            // Otherwise create the key.
            else
            {
                Dict.Add(key, value);
                valueChanged = true;
            }
            
            return valueChanged;
        }

        public static T GetValueOrDefault<T>(string key, T defaultValue)
        {
            if (Dict.ContainsKey(key))
                return (T)Dict[key];

            return defaultValue;
        }

        public static void Clear()
        {

        }

        public static void Save()
        {
            
        }
    }
#else
    /// <summary>
    /// A settings manager that abstracts the isolated storage to persist nonvolatile settings.
    /// </summary>
    public static class Settings
    {
        private static IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

        static Settings()
        {
            settings.Keys.ToString();
        }


        /// <summary>
        /// Adds or updates a key in the settings collection.  If the key exists, it is overwritten, and if it does not, it is added.
        /// </summary>
        /// <param name="Key">The key of the value.</param>
        /// <param name="value">The value to set.</param>
        /// <returns>Returns true if the value was changed or added.</returns>
        public static bool AddOrUpdateValue(string Key, Object value)
        {
            while (saveLock) ;
            saveLock = true;
            bool valueChanged = false;

            // If the key exists
            if (settings.Contains(Key))
            {
                // If the value has changed
                if (settings[Key] != value)
                {
                    // Store the new value
                    settings[Key] = value;
                    valueChanged = true;
                }
            }
            // Otherwise create the key.
            else
            {
                settings.Add(Key, value);
                valueChanged = true;
            }
            saveLock = false;
            return valueChanged;
        }

        /// <summary>
        /// Gets the value of the setting, or a default if it does not exist in the collection.
        /// </summary>
        /// <typeparam name="T">The type of the setting.</typeparam>
        /// <param name="key">The key of the setting to get.</param>
        /// <param name="defaultValue">The default value.</param>
        /// <returns>The value of the setting, or <paramref name="defaultValue"/> if <paramref name="key"/> does not exist in the collection.</returns>
        public static T GetValueOrDefault<T>(string key, T defaultValue)
        {
            while (saveLock) ;
            saveLock = true;
            T value;

            // If the key exists, retrieve the value.
            if (settings.Contains(key))
            {
                value = (T)settings[key];
            }
            // Otherwise, use the default value.
            else
            {
                value = defaultValue;
            }
            saveLock = false;
            return value;
        }

        /// <summary>
        /// Returns <value>true</value> if the key exists in the settings collection.
        /// </summary>
        /// <param name="key">The key to check for.</param>
        /// <returns>True if the key exists.</returns>
        public static bool ContainsKey(string key)
        {
            return settings.Contains(key);
        }

        /// <summary>
        /// Clears the settings collection.
        /// </summary>
        public static void Clear()
        {
            while (saveLock) ;
            saveLock = true;
            settings.Clear();
            settings.Save();
            saveLock = false;
        }

        /// <summary>
        /// A lock to prevent multiple threads from trying to save at the same time (causes problems).
        /// </summary>
        private static bool saveLock = false;

        /// <summary>
        /// Commits the changed settings to the isolated storage.
        /// </summary>
        public static void Save()
        {
            while (saveLock) ;
            saveLock = true;
            try
            {
                settings.Save();
            }
            catch { }
            saveLock = false;
        }
    }
#endif
}
